%%%%%%%%%%%%%%%%%%
%% SETUP THE MATLAB PATHS
% make sure that fieldtrip and spm are not in your matlab path

% SET THE BELOW LINE TO THE OSL DIRECTORY:
%tilde='/home/mwoolrich';
tilde='/Users/woolrich';

osldir=[tilde '/homedir/matlab/osl1.2.beta.14'];
addpath(osldir);
osl_startup(osldir);

%%%%%%%%%%%%%%%%%%
%% INITIALISE GLOBAL SETTINGS FOR THIS ANALYSIS

%testdir=['/Users/woolrich/homedir/matlab/osl_testdata_dir/faces_group_data_new']; % directory where the data is
testdir=['/Users/woolrich/Desktop/faces_group_data_new']; 

%%%%%%%%%%%%%%%%%%
%% load pre-run OAT analysis and run group-level stage for SENSOR space
%% analysis

% Load OAT analysis for which the first 2 stages (source recon and
% first-level GLM) have already been run.
% Note that the 1st level contrasts run are:
% S2.contrast{1}=[3 0 0 0]'; % motorbikes
% S2.contrast{2}=[0 1 1 1]'; % faces
% S2.contrast{3}=[-3 1 1 1]'; % faces-motorbikes

oatdir=[testdir '/faces_group_sensor_norm.oat'];
oat = osl_load_oat(oatdir); 

%% run OAT

oat.do_plots=0;
oat.to_do=[0 0 0 1];

oat = osl_run_oat(oat);

%% visualise group result using Fieldtrip
% note that this produces an interactive figure, with which you can:
% - draw around a set of sensors
% - click in the drawn box to produce a plot of the time series
% - on the time series plot you can draw a time window
% - and click in the window to create a topoplot averaged over that time
% window (which is itself interactive....!)

S2=[];
S2.oat=oat;
S2.stats_fname=oat.group_level.results_fnames;
S2.modality='MEGPLANAR';
S2.first_level_contrast=1:3;
S2.group_level_contrast=1;
S2.cfg.colorbar='yes';
S2.view_cope=0;

% calculate t-stat using contrast of absolute value of parameter estimates
[cfg, data]=osl_stats_multiplotER(S2);

